package com.portal.framework.util;

import java.text.DecimalFormat;
import java.util.Date;

public class ValueUtil {

	public static boolean isValueClass(Class cls) {
		if (ClassUtil.isMainClass(cls)) {
			return true;
		}

		if (cls == String.class) {
			return true;
		}

		if (cls == Class.class) {
			return true;
		}

		if (cls == Date.class) {
			return true;
		}

		return false;
	}

	public static Object toValue(Class to, String v) throws Exception {
		if(v==null||to==null){
			return null;
		}
		if (to == String.class) {
			return v;
		}
		if (ClassUtil.toMainClass(to) == Boolean.class) {
			return Boolean.valueOf(v);
		}
		if (ClassUtil.toMainClass(to) == Byte.class) {
			if (v.startsWith("0x")) {
				return Byte.valueOf(v.substring(2), 16);
			} else {
				return Byte.valueOf(v);
			}
		}
		if (ClassUtil.toMainClass(to) == Character.class) {
			Integer iv = null;
			if (v.startsWith("0x")) {
				iv = Integer.valueOf(v.substring(2), 16);
			} else {
				iv = Integer.valueOf(v);
			}
			return new Character((char) iv.intValue());
		}
		if (ClassUtil.toMainClass(to) == Short.class) {
			if (v.startsWith("0x")) {
				return Short.valueOf(v.substring(2), 16);
			} else {
				return Short.valueOf(v);
			}
		}
		if (ClassUtil.toMainClass(to) == Integer.class) {
			if (v.startsWith("0x")) {
				return Integer.valueOf(v.substring(2), 16);
			} else {
				return Integer.valueOf(v);
			}
		}
		if (ClassUtil.toMainClass(to) == Long.class) {
			if (v.startsWith("0x")) {
				return Long.valueOf(v.substring(2), 16);
			} else {
				return Long.valueOf(v);
			}
		}
		if (ClassUtil.toMainClass(to) == Float.class) {
			return Float.valueOf(v);
		}
		if (ClassUtil.toMainClass(to) == Double.class) {
			return Double.valueOf(v);
		}
		if (to == Class.class) {
			return ClassUtil.forName(v);
		}
		if (to == Date.class) {
			return DateTimeUtil.parseDate(v, new Date());
		}
		return null;
	}

	/**
	 * 得到基本类型的默认值<BR>
	 * 如果不为基本类型返回 null;
	 * 
	 * @param cls
	 * @return
	 */
	public static Object getDefaultValue(Class cls) {

		if (cls == null || !cls.isPrimitive()) {
			return null;
		}

		if (cls == Boolean.TYPE) {
			return (boolean) false;
		}

		if (cls == Byte.TYPE) {
			return (byte) 0;
		}

		if (cls == Character.TYPE) {
			return (char) 0;
		}

		if (cls == Short.TYPE) {
			return (short) 0;
		}

		if (cls == Integer.TYPE) {
			return (int) 0;
		}

		if (cls == Long.TYPE) {
			return (long) 0;
		}

		if (cls == Float.TYPE) {
			return (float) 0;
		}

		if (cls == Double.TYPE) {
			return (double) 0;
		}
		return null;
	}
	
	public static Object doubleToClass(Class cls, double value){		
		if(cls==String.class){
			DecimalFormat decFormat = new DecimalFormat("###0");
			return decFormat.format(value);
		}		
		if (cls == Short.class) {			
			return (short) value;
		}
		if (cls == Integer.class) {
			return (int) value;
		}
		if (cls == Long.class) {
			return (long) value;
		}
		if (cls == Date.class) {
			return new Date((long) value);
		}
		return value;
	}
}
